/**
 * @author Lion
 * @date 2017年1月24日 下午12:02:35
 * @qq 439635374
 */
package com.pointlion.sys.mobile.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.pointlion.sys.mvc.base.BaseController;
import com.pointlion.sys.mvc.org.SysOrg;
import com.pointlion.sys.mvc.user.SysUser;
import com.pointlion.sys.tool.DateUtil;

/***
 * 通知公告控制器（web）
 * @author Administrator
 *
 */

public class MobileController extends BaseController {
	
	static MobileService service =  MobileService.me;
	
	/***
	 * 获取新闻头条
	 */
	public void getBanner(){
		renderSuccess(service.getContentList("5"),"请求成功");
	}
	
	/***
	 * 获取某种类型的列表
	 */
	public void getContentList(){
		String type = getPara("type");//类型
		renderSuccess(service.getContentList(type),"请求成功");
	}
	
	/***
	 * 获取大事件
	 */
	public void getbigEvent(){
		Integer year = Integer.parseInt(DateUtil.getYear());
		List<Integer> yearList = new ArrayList<Integer>();
		List<List<Record>> contentList = new ArrayList<List<Record>>();
		Map<String , Object> data = new HashMap<String , Object>();
		Integer year1 = year-1;
		Integer year2 = year-2;
		Integer year3 = year-3;
		yearList.add(year);
		yearList.add(year1);
		yearList.add(year2);
		yearList.add(year3);
		contentList.add(Db.find("select * from cms_content t where t.type='1' and t.if_publish='1' and t.create_time like '"+year+"%'"));
		contentList.add(Db.find("select * from cms_content t where t.type='1' and t.if_publish='1' and t.create_time like '"+year1+"%'"));
		contentList.add(Db.find("select * from cms_content t where t.type='1' and t.if_publish='1' and t.create_time like '"+year2+"%'"));
		contentList.add(Db.find("select * from cms_content t where t.type='1' and t.if_publish='1' and t.create_time like '"+year3+"%'"));
		data.put("yearList", yearList);
		data.put("contentList", contentList);
		renderSuccess(data, "查询成功");
	}
	/***
	 * 获取专题专栏
	 */
	public void getSpecial(){
		renderSuccess(Db.find("select * from cms_group ORDER BY sort"), "查询成功");
	}
	/***
	 * 获取特定分组的专题专栏
	 */
	public void getSpecialContentList(){
		String groupid = getPara("groupid");
		renderSuccess(Db.find("select * from cms_content t where t.type='3' and t.group='"+groupid+"' and t.if_publish='1'"), "查询成功");
	}
	/***
	 * 获取内容
	 */
	public void getContent(){
		String id = getPara("id");//主键
		renderSuccess(service.getContent(id),"请求成功");
	}
	
	/*******************个人设置************************/
	public void getUser(){
		String userid = getPara("userid");
		SysUser user = SysUser.dao.getByUsername(userid);
		renderSuccess(user,"查询成功");
	}
	public void updateUser(){
		SysUser user = getModel(SysUser.class);
		if(StrKit.notBlank(user.getId())){
			user.update();
			renderSuccess();
		}else{
			renderError();
		}
	}
	/***************组织建设************************/
	/***
	 * 获取组织结构信息
	 */
	public void getOrg(){
		String orgid = getPara("orgid");
		renderSuccess(SysOrg.dao.getById(orgid),"查询成功");
	}
	/***
	 * 获取组织信息
	 */
	public void getOrgBuild(){
		String orgid = getPara("orgid");
		Map<String , Object> data = new HashMap<String ,Object>();
		SysOrg org = SysOrg.dao.getById(orgid);
		//查询下级党员数目
		int numZS = 0;
		int numLD = 0;
		if("#root".equals(org.getType())){//如果查询区域党委的数目
			//查询下级单位
			List<SysOrg> orgList = SysOrg.dao.getChildrenByPid(orgid);//党委列表（镇）
			data.put("orgList", orgList);
			if(orgList!=null&&orgList.size()>0){//
				for( SysOrg o :orgList){//循环所有党委（镇）
					List<SysOrg> orgList2 = SysOrg.dao.getChildrenByPid(o.getId());//党支部列表
					if(orgList2!=null&&orgList2.size()>0){//没有下级了
						for( SysOrg o2 :orgList2){//循环党支部列表
							//查询组织下党员数
							List<Record> userli1 = Db.find("select * from sys_user u where u.orgid='"+o2.getId()+"' and u.type='ZS'");
							if(userli1!=null){//正式党员
								numZS = numZS + userli1.size();
							}
							List<Record> userli2 = Db.find("select * from sys_user u where u.orgid='"+o2.getId()+"' and u.type='LD'");
							if(userli1!=null){//流动党员
								numLD = numLD + userli2.size();
							}
						}
					}
				}
			}
			data.put("numZS", numZS);
			data.put("numLD", numLD);
		}else if("1".equals(org.getType())){//党委
			//查询下级单位
			List<SysOrg> orgList2 = SysOrg.dao.getChildrenByPid(org.getId());//党支部列表
			data.put("orgList", orgList2);
			if(orgList2!=null&&orgList2.size()>0){//没有下级了
				for( SysOrg o2 :orgList2){//循环党支部列表
					//查询组织下党员数
					List<Record> userli1 = Db.find("select * from sys_user u where u.orgid='"+o2.getId()+"' and u.type='ZS'");
					if(userli1!=null){//正式党员
						numZS = numZS + userli1.size();
					}
					List<Record> userli2 = Db.find("select * from sys_user u where u.orgid='"+o2.getId()+"' and u.type='LD'");
					if(userli1!=null){//流动党员
						numLD = numLD + userli2.size();
					}
				}
			}
			data.put("numZS", numZS);
			data.put("numLD", numLD);
		}else{//党支部
			List<Record> userlist = Db.find("select * from sys_user u where u.orgid='"+org.getId()+"'");
			data.put("userList", userlist);
			//查询组织下党员数
			List<Record> userli1 = Db.find("select * from sys_user u where u.orgid='"+org.getId()+"' and u.type='ZS'");
			if(userli1!=null){//正式党员
				numZS = numZS + userli1.size();
			}
			List<Record> userli2 = Db.find("select * from sys_user u where u.orgid='"+org.getId()+"' and u.type='LD'");
			if(userli1!=null){//流动党员
				numLD = numLD + userli2.size();
			}
			data.put("numZS", numZS);
			data.put("numLD", numLD);
		}
		data.put("org", org);
		renderSuccess(data,"查询成功");
	}
}
